package cn.fl.institution.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.api.IPrjCstAccInfoApiService;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import cn.fl.project.dto.PrjBscInfoDTO;
import cn.fl.project.service.IPrjCstAccInfoService;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.transform.PrjBscInfoVOTransform;
import cn.fl.project.utils.CommonUtils;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjCstAccInfoVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description: 项目基础信息表 控制层 天津银行 tianjinbank
 */
@Controller
@RequestMapping("/institution/tianjinbank")
public class PrjBscInfoInsTianjinbankController {

    @Autowired
    ICstNpBscInfoService cstNpBscInfoService; // 自然人信息

    @Autowired
    IProjectApiService projectApiService;

    @Autowired
    private IPrjCstAccInfoService prjCstAccInfoService;//项目客户基础信息表

    /**
     * 保存 项目客户信息
     */
    @RequestMapping(value = "/tianjinBankPrjAccInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public DataResultVO tianjinBankPrjAccInfo(HttpServletRequest request) throws BaseException {
        BaseLogUtils.newLogger("天津银行信息采集新增修改Start,request:{}" , JSON.toJSONString(request.getParameterMap())).info();
        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "保存数据成功");
        String prjId = request.getParameter("prjId");
        String insCode = request.getParameter("insCode");
        String cstId = request.getParameter("cstId");
        String cstNm = request.getParameter("cstNm");
        String certNo = request.getParameter("certNo");
        String domicileProvince = request.getParameter("domicileProvince");
        String domicileCity = request.getParameter("domicileCity");
        String domicileCounty = request.getParameter("domicileCounty");
        String postProvince = request.getParameter("postProvince");
        String postCity = request.getParameter("postCity");
        String livCndCd = request.getParameter("livCndCd");
        try {
            if (StringUtils.isBlank(prjId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "项目ID为空!");
            }
            if (StringUtils.isBlank(cstId)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户ID为空!");
            }
            if (StringUtils.isBlank(domicileProvince)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "户籍所在地省为空!");
            }
            if (StringUtils.isBlank(domicileCity)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "户籍所在地市为空!");
            }
            if (StringUtils.isBlank(domicileCounty)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "户籍所在地区县为空!");
            }
            if (StringUtils.isBlank(postProvince)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "居住省空!");
            }
            if (StringUtils.isBlank(postCity)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "居住市为空!");
            }
            if (StringUtils.isBlank(livCndCd)) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "居住状况为空!");
            }

            //根据id查询项目信息
            BaseLogUtils.newLogger("天津银行保存信息,查询项目信息Start,prjId:{}", prjId).info();
            PrjBscInfoDTO prjBscInfoDTO = this.projectApiService.selectById(Long.valueOf(prjId), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
            BaseLogUtils.newLogger("天津银行保存信息,查询项目信息End,prjId:{},prjBscInfoDTO:{}", prjId, JSON.toJSONString(prjBscInfoDTO)).info();
            if (null == prjBscInfoDTO) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "查询项目为空!");
            }
            if (!cstId.equals(String.valueOf(prjBscInfoDTO.getCstId()))) {
                return new DataResultVO(BaseConstant.IS_YESNO_NO, "客户ID不匹配!");
            }
            BaseLogUtils.newLogger("天津银行保存信息,获取自然人信息Start,prjId:{},cstId:{}", prjId, prjBscInfoDTO.getCstId()).info();
            CstNpBscInfoVO cstNpBscInfoVO = this.cstNpBscInfoService.selectById(prjBscInfoDTO.getCstId());
            BaseLogUtils.newLogger("天津银行保存信息,获取自然人信息End,prjId,cstId:{},cstNpBscInfoVO:{}", prjId, prjBscInfoDTO.getCstId(), JSON.toJSONString(cstNpBscInfoVO)).info();
            if (null != cstNpBscInfoVO) {
                boolean flag = false;
                if (!Objects.equals(domicileProvince, cstNpBscInfoVO.getDomicileProvince())) {
                    cstNpBscInfoVO.setDomicileProvince(domicileProvince);
                    flag = true;
                }
                if (!Objects.equals(domicileCity, cstNpBscInfoVO.getDomicleCity())) {
                    cstNpBscInfoVO.setDomicleCity(domicileCity);
                    flag = true;
                }
                if (!Objects.equals(domicileCounty, cstNpBscInfoVO.getDomicleCounty())) {
                    cstNpBscInfoVO.setDomicleCounty(domicileCounty);
                    flag = true;
                }
                if (!Objects.equals(postProvince, cstNpBscInfoVO.getPostProvince())) {
                    cstNpBscInfoVO.setPostProvince(postProvince);
                    flag = true;
                }
                if (!Objects.equals(postCity, cstNpBscInfoVO.getPostCity())) {
                    cstNpBscInfoVO.setPostCity(postCity);
                    flag = true;
                }
                if (!Objects.equals(livCndCd, cstNpBscInfoVO.getLivCndCd())) {
                    cstNpBscInfoVO.setLivCndCd(livCndCd);
                    flag = true;
                }
                BaseLogUtils.newLogger("天津银行保存信息,是否保存自然人信息,prjId:{},flag:{}", prjId, flag).info();
                if (flag) {
                    BaseLogUtils.newLogger("天津银行保存信息,保存自然人信息Start,prjId:{},cstNpBscInfoVO:{}", prjId, JSON.toJSONString(cstNpBscInfoVO)).info();
                    boolean modify = this.cstNpBscInfoService.modify(cstNpBscInfoVO);
                    BaseLogUtils.newLogger("天津银行保存信息,保存自然人信息End,prjId:{},modify:{}", prjId, modify).info();
                }
            }
        } catch (BaseException ex) {
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(ex.getMessage());
            BaseLogUtils.newLogger("天津银行保存信息,异常:prjId:{},ex:{}" , prjId, JSONObject.toJSONString(ex.getMessage())).info();
        } catch (Exception e) {
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("天津银行保存信息,系统异常");
            BaseLogUtils.newLogger("天津银行保存信息,系统异常:prjId:{},e:{}" , prjId,JSONObject.toJSONString(e.getMessage())).error(BaseExceptionConstant.INTERFACE_0102);
            return resultVO;
        }
        BaseLogUtils.newLogger("天津银行保存信息End,prjId:{},resultVO:{}", prjId, JSONObject.toJSONString(resultVO)).info();
        return resultVO;
    }
}
